package com.aistar.service.impl;

import com.aistar.mapper.ProductReviewImagesMapper;
import com.aistar.mapper.ProductReviewMapper;
import com.aistar.pojo.ProductImages;
import com.aistar.pojo.ProductReview;
import com.aistar.pojo.ProductReviewImages;
import com.aistar.pojo.ProductReviewImagesExample;
import com.aistar.service.EvaluationService;
import com.aistar.util.ServerResponse;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class EvaluationServiceImpl implements EvaluationService {

    @Autowired
    ProductReviewMapper reviewMapper;

    @Autowired
    ProductReviewImagesMapper reviewImagesMapper;

    /**
     * 添加产品评论
     * @param cusId
     * @param proId
     * @param grade
     * @param type
     * @return
     */
    @Override
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    public ServerResponse addEvaluation(Integer cusId, Integer proId, Integer grade, String token, String type,String content) {

        ProductReview productReview = new ProductReview();
        //赋值
        productReview.setCustomerId(cusId);
        productReview.setProductId(proId);
        productReview.setProductReviewStar(grade);
        productReview.setProductReviewType(type);
        productReview.setProductReviewContent(content);
        productReview.setAgreeCount(0);
        productReview.setProductReviewTime(new Date());
        productReview.setAnswerCount(0);
        productReview.setStatus("1");
        productReview.setOther1(token);

        int rows = reviewMapper.insert(productReview);

        if(rows == 0){
            return ServerResponse.addFailed();
        }else {
            return ServerResponse.addSuccess();
        }
    }

//    /**
//     * 添加评论图片
////     * @param evaId
//     * @param imgUrls
//     * @return
//     */
//    @Override
//    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
//    public ServerResponse addEvaluationImg(List<String> imgUrls,String token) {
//        ServerResponse serverResponse = new ServerResponse();
//
//        imgUrls.forEach(imgUrl -> {
//
//            ProductReviewImages productReviewImages = new ProductReviewImages();
//            productReviewImages.setProductReviewId(701);
//            productReviewImages.setUrl(imgUrl);
//            productReviewImages.setStatus("1");
//            productReviewImages.setOther1(token);
//
//            reviewImagesMapper.insert(productReviewImages);
//        });
//
//        ProductReviewImagesExample productReviewImagesExample = new ProductReviewImagesExample();
//        productReviewImagesExample.createCriteria().andOther1EqualTo(token).andStatusEqualTo("1");
//
//        List<ProductReviewImages> images = reviewImagesMapper.selectByExample(productReviewImagesExample);
//        if (images != null && images.size() > 0)
//            return ServerResponse.addSuccess();
//        else
//            return ServerResponse.addFailed();
//    }

    @Override
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    public ServerResponse getAll() {
        return null;
    }
}
